/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.calendar.weekview;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import mtt.calendar.eventeditor.EventEditorController;
import mtt.calendar.eventeditor.EventEditorView;
import mtt.calendar.eventeditor.events.EditEventEvent;
import mtt.calendar.eventeditor.events.ShowEventEditorView;
import mtt.calendar.weekview.events.ShowWeekEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.rms.dataObjects.Event;
import mtt.rms.recordstores.calendar.Week;

import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.TerminateEvent;
import hmvc.j2me.view.AView;

/**
 *
 *
 * @author jens.meiss
 */
public class WeekView extends AView {
	
	private Form form = new Form("MTT - Wochenübersicht");
	
	private Command commandBack = new Command("zurück", "zurück zum Hauptmenü", Command.BACK, 0);
	
	private Command commandEdit = new Command("Bearbeiten", "Eintrag bearbeiten", Command.SCREEN, 1);
	private Command commandAdd =  new Command("Hinzufügen", "Eintrag hinzufügen", Command.SCREEN, 1);
	private Command commandDelete =  new Command("Löschen", "Eintrag Löschen", Command.SCREEN, 1);

	public void handleEvent(IEvent event) {
		if (event instanceof ShowWeekEvent) {
			ShowWeekEvent showWeekEvent = (ShowWeekEvent) event;
			showWeek(showWeekEvent.getWeek());
		}
	}

	private void showWeek(Week week) {
		
		byte cnt = 0;
		while (cnt < 7) {
			
			String tag = null;
			switch (cnt) {
			case 0:
				tag = "Montag";
				break;
			case 1:
				tag = "Dinstag";
				break;
			case 2:
				tag = "Mittwoch";
				break;
			case 3:
				tag = "Donnerstag";
				break;
			case 4:
				tag = "Freitag";
				break;
			case 5:
				tag = "Samstag";
				break;
			case 6:
				tag = "Sonntag";
				break;
			}
			
			
			form.append(new StringItem("Tag", tag));
			
			Vector vector = week.getVectorEvents(cnt);
			
			if (vector.size() == 0) {
				form.append(new StringItem("Termine", "keine Termine vorhanden"));
			} else {
				Enumeration enumeration = vector.elements();
				while (enumeration.hasMoreElements()) {
					Event event = (Event) enumeration.nextElement();
					
					form.append(new StringItem(event.getName(),  event.getPlace()));
				}
			}
			cnt++;
		}
	}

	public void terminate() {
	}

	public void init() {
		
		form.addCommand(commandBack);
		form.addCommand(commandAdd);
		form.addCommand(commandDelete);
		form.addCommand(commandEdit);
		
		setDisplayAble(form);
	}

	public void registerEvents() {
		getController().register(ShowWeekEvent.class, this);
	}

	public void commandAction(Command c, Displayable d) {
		
		if (c == commandBack) {
			getController().fireEventUp(new ShowMainMenuEvent());
			getController().fireEventLocal(new TerminateEvent());
		} else if (c == commandAdd) {
			getController().addChildComponent(new EventEditorController(), new EventEditorView());
			getController().fireEventDown(new EditEventEvent(new Event()));
			getController().fireEventDown(new ShowEventEditorView());
		} else if (c == commandEdit) {
			
		} else if (c == commandDelete) {
			
		}
		
	}

}
